package com.ainiyar.springweb.controller.admin;

import com.ainiyar.springweb.common.result.ResultResponse;
import com.ainiyar.springweb.pojo.admin.dto.PageDto;
import com.ainiyar.springweb.pojo.admin.vo.SelectVo;
import com.ainiyar.springweb.pojo.entity.Admin;
import com.ainiyar.springweb.pojo.entity.Role;
import com.ainiyar.springweb.service.AdminService;
import com.ainiyar.springweb.service.RoleService;
import com.ainiyar.springweb.util.ToolsUtil;
import jakarta.annotation.Resource;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**************************************
 * File Name : RoleController
 * Created with : IntelliJ IDEA.
 * Author : 艾尼亚尔·毛拉吾提
 * QQ : 524155792  WeChat : ainiyar0124
 * Created Time : 2024/2/5  02:09
 * Desc :角色管理控制器
 ***************************************/
@RestController
@RequestMapping("/role")
public class RoleController {
    @Resource
    private RoleService roleService;
    @Resource
    private AdminService adminService;

    /**
     * 角色列表
     *
     * @param page 分页参数
     * @param name 查询条件
     * @return Object
     */
    @GetMapping("/list")
    @PreAuthorize("hasAuthority('role:list')")
    public ResultResponse<Object> lists(PageDto page, String name, Integer status) {
        return roleService.lists(page, name, status);
    }

    /**
     * 新增角色
     *
     * @param role 角色参数
     * @return string
     */
    @PostMapping("/add")
    @PreAuthorize("hasAuthority('role:add')")
    public ResultResponse<String> addRole(Role role) {
        if (ToolsUtil.isNullOrEmpty(role.getName())) {
            return ResultResponse.Fail("角色名称不得为空");
        }
        if (role.getPermissionList().isEmpty()) {
            return ResultResponse.Fail("角色权限不得为空");
        }
        boolean add = roleService.saveAddEdit(role);
        if (add) {
            return ResultResponse.Success("新增角色成功");
        }
        return ResultResponse.Fail("新增角色失败");
    }

    /**
     * 修改角色
     *
     * @param role 角色参数
     * @return string
     * @throws Exception 事务异常
     */
    @PutMapping("/edit")
    @PreAuthorize("hasAuthority('role:edit')")
    public ResultResponse<String> editRole(Role role) throws Exception {
        if (ToolsUtil.isNullOrEmpty(role.getName())) {
            return ResultResponse.Fail("角色名称不得为空");
        }
        if (role.getPermissionList().isEmpty()) {
            return ResultResponse.Fail("角色权限不得为空");
        }
        boolean edit = roleService.saveAddEdit(role);
        if (edit) {
            return ResultResponse.Success("修改角色成功");
        }
        return ResultResponse.Fail("修改角色失败");
    }

    /**
     * 删除角色
     *
     * @param id 角色id
     */
    @DeleteMapping("/del/{id}")
    @PreAuthorize("hasAuthority('role:del')")
    public ResultResponse<String> deleteRole(@PathVariable Long id) {
        //检查当前角色是否已绑定管理员
        List<Admin> hasBindAdmin = adminService.findAdminByRoleId(id);
        if (!hasBindAdmin.isEmpty()) {
            return ResultResponse.Fail("角色删除失败、当前角色已绑定管理员，请先解绑、再进行删除");
        }
        if (roleService.del(id)) {
            return ResultResponse.Success("角色删除成功");
        }
        return ResultResponse.Fail("角色删除失败、请稍后再试试");
    }

    /**
     * 为下拉框
     */
    @GetMapping("/get_role")
    public ResultResponse<List<SelectVo>> getRoleForSelect(){
        return ResultResponse.Result(roleService.getRoleForSelect());
    }
}

